import { addHospitaSet, getHospitalSet, updateHospitalSet } from '@/api/hospital/hospitalSet';
import { Button, Card, Form, Input, Space, message } from 'antd'
import React, { useEffect } from 'react'
import { useNavigate, useParams } from 'react-router-dom';

export default function AddOrUpdate() {
    // 编辑时获取 path 参数 id
    let { id } = useParams()

    // 创建form对象
    const [form] = Form.useForm();
    // navigate
    const navigate = useNavigate();
    const onFinish = async () => {
        let data = form.getFieldsValue();
        
        try {
            if (id) {// 编辑
                data.id = id;
                await updateHospitalSet(data);
                message.success('编辑成功');
            } else {// 添加
                // 发送请求添加医院设置
                await addHospitaSet(data);
                // 提示成功信息
                message.success('添加成功');
            }
            // 跳转到医院设置列表
            navigate('/syt/hospital/hospitalSet');
        } catch (e: any) {
            message.error(e.message)
        }
    }

    // 根据id获取医院设置
    async function _getHospitalSet() {
        // 发送请求
        let res = await getHospitalSet(id as string)
        // form.setFieldsValue({
        //     hosname:'123123',
        //     hoscode:'666'
        // })
        form.setFieldsValue(res);
        console.log('res: ', res);
    }
    // componentDidMount
    useEffect(() => {
        // 如果id存在，证明是编辑操作，那么返送请求获取编辑数据
        id && _getHospitalSet();
    }, [])
    return (
        <Card>
            <Form
                form={form}
                name="basic"
                labelCol={{ span: 2 }}
                wrapperCol={{ span: 22 }}
                onFinish={onFinish}
            >
                <Form.Item
                    label="医院名称"
                    name="hosname"
                    rules={[{ required: true, message: '请添加医院名称!' }]}
                >
                    <Input />
                </Form.Item>

                <Form.Item
                    label="医院编号"
                    name="hoscode"
                    rules={[{ required: true, message: '请填写医院编号!' }]}
                >
                    <Input />
                </Form.Item>

                <Form.Item
                    label="api基础路径"
                    name="apiUrl"
                    rules={[{ required: true, message: '请填写医院编号!' }]}
                >
                    <Input />
                </Form.Item>
                <Form.Item
                    label="联系人姓名"
                    name="contactsName"
                    rules={[{ required: true, message: '请填写医院编号!' }]}
                >
                    <Input />
                </Form.Item>
                <Form.Item
                    label="联系人手机"
                    name="contactsPhone"
                    rules={[{ required: true, message: '请填写医院编号!' }]}
                >
                    <Input />
                </Form.Item>
                <Form.Item wrapperCol={{ offset: 2, span: 16 }}>
                    <Space>
                        <Button type="primary" htmlType="submit">
                            保存
                        </Button>
                        <Button htmlType="submit">
                            返回
                        </Button>
                    </Space>
                </Form.Item>
            </Form>
        </Card>
    )
}
